import { Application } from "egg";
import { ObjectId } from "mongoose";
import * as AutoIncrementFactory from "mongoose-sequence";
export interface FieldProps {
  name: string;
  id: string;
}
export interface BasicFieldProps {
  name: string; // 分组名称
  fields?: FieldProps[]; // 字段列表
  company_id: ObjectId; // 所属公司
  is_delete: boolean; // 是否删除
}
function initBasicFieldModel(app: Application) {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;
  const AutoIncrement = AutoIncrementFactory(mongoose);
  const BasicFieldScheme = new Schema<BasicFieldProps>(
    {
      name: { type: String, unique: true, required: true },
      fields: { type: Array },
      company_id: {
        type: Schema.Types.ObjectId,
        ref: "Company",
        required: true,
      },
      is_delete: { type: Boolean, default: false },
    },
    {
      timestamps: true,
      toJSON: {
        transform(_doc, ret) {
          delete ret.__v;
        },
      },
    }
  );
  BasicFieldScheme.plugin(AutoIncrement, {
    inc_field: "id",
    id: "BasicField_counter",
  });
  return mongoose.model<BasicFieldProps>("BasicField", BasicFieldScheme);
}

export default initBasicFieldModel;
